﻿#region Using Statements
using System;
using System.Windows.Forms;
using System.IO;
using System.Collections.Generic;
using System.Text;

#endregion
public class Nastr
{
    string fullfilename { get { return Environment.CurrentDirectory + "\\ClientNastr.set"; } }
    public string y_reg { get; set; }
    public string g_reg { get; set; }
    public string server_adr { get; set; }
    public int max_count { get; set; }
    public int google { get; set; }
    public int yandex { get; set; }
    public int mess_time { get; set; }
    public Language language { get; set; }
    public bool log { get; set; }
    void GetDef()
    {
        y_reg = @"<b class=\u0022b-serp-item__number\u0022>(?<pos>\d*)</b>.*?href=\u0022(?<home>.*?)\u0022";
        g_reg = @"<span class=tl><h3 class=\u0022r\u0022><a href=\u0022(?<home>.*?)\u0022.*?'(?<pos>\d+)'?,";
        server_adr = @"127.0.0.1";
        max_count =500;//глубина поиска
        google = 250; //задержка в мс
        yandex = 250;
        language = Language.Русский;
        mess_time = 1000;//время до скрытия сообщения
        log = true;
    }
    public Nastr()
    {
        if (File.Exists(fullfilename))
        {
            try
            {
                byte[] b = File.ReadAllBytes(fullfilename);
                using (MemoryStream ms = new MemoryStream(b))
                using (BinaryReader br = new BinaryReader(ms))
                {
                    y_reg = BYTE.ToShortString(br);
                    g_reg = BYTE.ToShortString(br);
                    server_adr = BYTE.ToShortString(br);
                    max_count = br.ReadInt32();
                    google = br.ReadInt32();
                    yandex = br.ReadInt32();
                    language = (Language)br.ReadByte();
                    mess_time = br.ReadInt32();
                    log = BYTE.ToStruct<bool>(br);
                }
            }
            catch (Exception e)
            {
                //throw new Exception(e.Message);
                GetDef();
            }
        }
        else
            GetDef();
    }

    public byte[] ToByte
    {
        get
        {
            List<byte>  b = new List<byte>();
            b.AddRange(ToBYTE.ShortString(y_reg));
            b.AddRange(ToBYTE.ShortString(g_reg));
            b.AddRange(ToBYTE.ShortString(server_adr));
            b.AddRange(BitConverter.GetBytes(max_count));
            b.AddRange(BitConverter.GetBytes(google));
            b.AddRange(BitConverter.GetBytes(yandex));
            b.Add((byte)language);
            b.AddRange(BitConverter.GetBytes(mess_time));
            b.AddRange(ToBYTE.Struct(log));
            return b.ToArray();
        }
    }

    public void Save()
    {
        using (FileStream stream = File.Open(fullfilename, FileMode.OpenOrCreate, FileAccess.Write, FileShare.None))
        {
            byte[] b = ToByte;
            stream.Write(b, 0, b.Length);
        }
    }
}

